//
//  YYIVCTemplateViewController.m
//  CodeGenerator
//
//  Created by 丁玉松 on 2017/9/13.
//  Copyright © 2017年 Beijing Yingyan Internet Co., Ltd. All rights reserved.
//

#import "YYIVCTemplateViewController.h"

@interface YYIVCTemplateViewController ()

@end

@implementation YYIVCTemplateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
}

-(void)writeData:(NSString *)dataStr toPath:(NSString *)filePath anchor:(NSString *)anchor {
    
    
    @"\
    \n  [self yyi_initData];\
    \n  [self yyi_initUI];\
    \n  [self yyi_loadData];\
\n}\
\n\
\n#pragma mark - 初始化数据\
\n\
\n-(void)yyi_initData{\n\
\n\
\n}\
\n\
\n#pragma mark - 初始化界面\
\n\
\n-(void)yyi_initUI{\
\n    \
\n}\
\n\
\n#pragma mark - 加载数据\
\n\
\n-(void)yyi_loadData{\
\n    \
\n}\
\n\
";
    
    
    //找到锚点
    NSString *strContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSRange range =  [strContent rangeOfString:anchor];
    
    NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];

    NSString *subStr = [strContent substringWithRange:NSMakeRange(range.location, strContent.length-range.location)];
    NSData *subStrData = [subStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"range:%@",NSStringFromRange(range));
    NSFileHandle* fh=[NSFileHandle fileHandleForWritingAtPath:filePath];
    
    [fh seekToFileOffset:range.location];
    [fh writeData:data];
    [fh writeData:subStrData];
    [fh closeFile];
}


@end
